import { IFeidaoAiBrowserPage } from '@feidao/web/interfaces';
import nodejs from '@feidao/web/atom/browser/msg/nodejs';
import { send_msg, client_url, spaceid, systemid, get_user_info } from '../atom-web/config';
import pay from '@feidao/web/atom/wx/web/js-sdk/pay';
import config from '@feidao/web/atom/wx/web/js-sdk/config';
import whoami from '@feidao/web/atom/browser/msg/whoami';

export default async function a001(fd: IFeidaoAiBrowserPage, ...args: any[]) {
	// 微信支付
	// 获取基本参数
	const code = fd.data.params.code;
	alert('--------');
	if (code) {
		// 微信公众号配置
		const rc = await config(send_msg, spaceid, ['chooseWXPay'], true);
		if (rc) {
			const state = fd.data.params.state;
			const info = JSON.parse(state);
			// 微信授权附带参数，内容取决于授权中state的参数值
			const doccode = info.doccode;
			const sessionid = info.sessionid;

			// 当前登录的用户信息
			const user_info = await whoami(get_user_info);

			// 调用服务获取 商品基本信息
			const res = await nodejs<{ fee: string; des: string; time_start: string; time_expire: string; }>(send_msg, 'get-pay-info', { doccode }, false, false);

			// 调用支付接口
			const r = await pay(client_url, send_msg, spaceid, systemid, code, user_info.usercode, 'weixin', doccode, res.fee, res.des, res.time_start, res.time_expire, { sessionid: user_info.sessionID, spaceid });
			if (r) {
				// 支付成功
			} else {
				// 支付失败
			}
		}
	}
}
